package utils

// 自定义错误结构体
type MyError struct {
	Code    int64  `json:"code"`
	Message string `json:"message"`
}

// 实现 Error() 方法
func (e *MyError) Error() string {
	return e.Message
}

// 创建自定义错误
func NewMyError(code int64, msg string) *MyError {
	return &MyError{
		Code:    code,
		Message: msg,
	}
}

type Response struct {
	Code    int64       `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func SuccessResponse(data interface{}) *Response {
	return &Response{
		//Code:    constants.CodeSuccess.Code, //import cycle not allowed
		Code:    200000,
		Message: "请求成功",
		Data:    data,
	}
}
func ErrorResponse(code int64, msg string) *Response {
	return &Response{
		Code:    code,
		Message: msg,
		Data:    nil,
	}
}

func Fail(err *MyError) *Response {
	return &Response{
		Code:    err.Code,
		Message: err.Message,
		Data:    nil,
	}
}
